<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>API usage | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = '_api_usage.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/_api_usage.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/_api_usage.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/_api_usage.html" rel="nofollow" target="_blank">../en/_api_usage.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="xpack-sql.html">SQL access</a></span>
»
<span class="breadcrumb-link"><a href="sql-jdbc.html">SQL JDBC</a></span>
»
<span class="breadcrumb-node">API usage</span>
</div>
<div class="navheader">
<span class="prev">
<a href="sql-jdbc.html">« SQL JDBC</a>
</span>
<span class="next">
<a href="sql-odbc.html">SQL ODBC »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="_api_usage"></a>API usage<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/endpoints/jdbc.asciidoc">edit</a>
</h2>
</div></div></div>
<p>One can use JDBC through the official <code class="literal">java.sql</code> and <code class="literal">javax.sql</code> packages:</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="java-sql"></a><code class="literal">java.sql</code><a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/endpoints/jdbc.asciidoc">edit</a>
</h3>
</div></div></div>
<p>The former through <code class="literal">java.sql.Driver</code> and <code class="literal">DriverManager</code>:</p>
<div class="pre_wrapper lang-java">
<pre class="programlisting prettyprint lang-java">String address = "jdbc:es://" + elasticsearchAddress;     <a id="CO64-1"></a><i class="conum" data-value="1"></i>
Properties connectionProperties = connectionProperties(); <a id="CO64-2"></a><i class="conum" data-value="2"></i>
Connection connection =
    DriverManager.getConnection(address, connectionProperties);</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO64-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>The server and port on which Elasticsearch is listening for
HTTP traffic. The port is by default 9200.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO64-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>Properties for connecting to Elasticsearch. An empty <code class="literal">Properties</code>
instance is fine for unsecured Elasticsearch.</p>
</td>
</tr>
</table>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="javax-sql"></a><code class="literal">javax.sql</code><a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/endpoints/jdbc.asciidoc">edit</a>
</h3>
</div></div></div>
<p>Accessible through the <code class="literal">javax.sql.DataSource</code> API:</p>
<div class="pre_wrapper lang-java">
<pre class="programlisting prettyprint lang-java">EsDataSource dataSource = new EsDataSource();
String address = "jdbc:es://" + elasticsearchAddress;     <a id="CO65-1"></a><i class="conum" data-value="1"></i>
dataSource.setUrl(address);
Properties connectionProperties = connectionProperties(); <a id="CO65-2"></a><i class="conum" data-value="2"></i>
dataSource.setProperties(connectionProperties);
Connection connection = dataSource.getConnection();</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO65-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>The server and port on which Elasticsearch is listening for
HTTP traffic. By default 9200.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO65-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>Properties for connecting to Elasticsearch. An empty <code class="literal">Properties</code>
instance is fine for unsecured Elasticsearch.</p>
</td>
</tr>
</table>
</div>
<p>Which one to use? Typically client applications that provide most
configuration properties in the URL rely on the <code class="literal">DriverManager</code>-style
while <code class="literal">DataSource</code> is preferred when being <em>passed</em> around since it can be
configured in one place and the consumer only has to call <code class="literal">getConnection</code>
without having to worry about any other properties.</p>
<p>To connect to a secured Elasticsearch server the <code class="literal">Properties</code>
should look like:</p>
<div class="pre_wrapper lang-java">
<pre class="programlisting prettyprint lang-java">Properties properties = new Properties();
properties.put("user", "test_admin");
properties.put("password", "x-pack-test-password");</pre>
</div>
<p>Once you have the connection you can use it like any other JDBC
connection. For example:</p>
<div class="pre_wrapper lang-java">
<pre class="programlisting prettyprint lang-java">try (Statement statement = connection.createStatement();
        ResultSet results = statement.executeQuery(
              " SELECT name, page_count"
            + "    FROM library"
            + " ORDER BY page_count DESC"
            + " LIMIT 1")) {
    assertTrue(results.next());
    assertEquals("Don Quixote", results.getString(1));
    assertEquals(1072, results.getInt(2));
    SQLException e = expectThrows(SQLException.class, () -&gt;
        results.getInt(1));
    assertThat(e.getMessage(), containsString("Unable to convert "
            + "value [Don Quixote] of type [TEXT] to [Integer]"));
    assertFalse(results.next());
}</pre>
</div>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>Elasticsearch SQL doesn’t provide a connection pooling mechanism, thus the connections
the JDBC driver creates are not pooled. In order to achieve pooled connections,
a third-party connection pooling mechanism is required. Configuring and setting up the
third-party provider is outside the scope of this documentation.</p>
</div>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="sql-jdbc.html">« SQL JDBC</a>
</span>
<span class="next">
<a href="sql-odbc.html">SQL ODBC »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>